#include "RefractionShader.h"


RefractionShader::RefractionShader(void)
{
	m_Effect = 0;
	m_Technique = 0;
	m_VertexLayout = 0;
}


RefractionShader::~RefractionShader(void)
{
}


bool RefractionShader::Initialize(ID3D11Device* pd3dDevice)
{
	ID3D10Blob*	pBlob = NULL;
	ID3D10Blob*	pErrorBlob = NULL;
	
    DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;

	// Compile .fx
	HRESULT hr = D3DX11CompileFromFile(L"refraction.fx", NULL, NULL,NULL, "fx_5_0", dwShaderFlags, 0,
                                         NULL, &pBlob, &pErrorBlob, NULL);

    if( FAILED( hr ) )
    {
		MessageBox(NULL, L"Problema nel caricamento di refraction.fx.", L"Error", MB_OK );

		if (pErrorBlob)
		{
			MessageBoxA(0, (char*)pErrorBlob->GetBufferPointer(), 0, 0);
		}
		return false;
    }

	D3DX11CreateEffectFromMemory(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, pd3dDevice, &m_Effect);

	m_Technique = m_Effect->GetTechniqueByName( "RefractionTechnique" );

	D3D11_INPUT_ELEMENT_DESC layout[] =
	{
		{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"NORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"TANGENT",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"BINORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"TEXCOORD",   0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
	};
    UINT numElements = sizeof( layout ) / sizeof( layout[0] );

	// Input layout
    D3DX11_PASS_DESC PassDesc;
    m_Technique->GetPassByIndex( 0 )->GetDesc( &PassDesc );
    hr = pd3dDevice->CreateInputLayout(layout, numElements, PassDesc.pIAInputSignature,
                                          PassDesc.IAInputSignatureSize, &m_VertexLayout );

	if(FAILED(hr))
		return false;
	
	return true;
}


void RefractionShader::setShaderParameters(D3DXMATRIX worldMatrix, D3DXMATRIX viewMatrix, D3DXMATRIX projectionMatrix,
	ID3D11ShaderResourceView* shaderTexture, D3DXVECTOR3 lightDirection, D3DXVECTOR4 ambientColor, D3DXVECTOR4 diffuseColor, D3DXVECTOR4 clipPlane)
{

	m_Effect->GetVariableByName("World")->AsMatrix()->SetMatrix(worldMatrix);

	m_Effect->GetVariableByName("View")->AsMatrix()->SetMatrix(viewMatrix);

    m_Effect->GetVariableByName("Projection")->AsMatrix()->SetMatrix(projectionMatrix);

	m_Effect->GetVariableByName("ShaderTexture")->AsShaderResource()->SetResource(shaderTexture);

	m_Effect->GetVariableByName("LightDirection")->AsVector()->SetFloatVector(lightDirection);
	
	m_Effect->GetVariableByName("AmbientColor")->AsVector()->SetFloatVector(ambientColor);

	m_Effect->GetVariableByName("DiffuseColor")->AsVector()->SetFloatVector(diffuseColor);

	m_Effect->GetVariableByName("ClipPlane")->AsVector()->SetFloatVector(clipPlane);
}



